package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thinkingstar.iads.datacenter.entity.DcManufacturerEquipment;
import org.nutz.dao.*;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.bstek.urule.runtime.response.RuleExecutionResponse;
import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.SwapUtil;
import com.thinkingstar.iads.datacenter.entity.DcEquipment;
import com.thinkingstar.iads.datacenter.entity.DcEquipmentEquip;


/**
 * @ClassName: DaoDcEquipmentEquip 
 * @Description:数据中心_项目_项目设备配备表维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcEquipmentEquip extends EntityRepositoryServiceImpl<DcEquipmentEquip>{

	public DaoDcEquipmentEquip(Dao dao) {
		super(dao);
	}

	//根据 设备配备库名称 查询 设备配备表
	public DcEquipmentEquip fineEquipmentByName(String sbpbkmc,String phase_id,String content_id) {
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false).and("project_design_phase_id", "=", phase_id)
				.and("project_design_content_id", "=", content_id)
				.and("equipment_equip_name","like","%"+sbpbkmc+"%");
		List <DcEquipmentEquip> query_list = query(criteria);
		if (null!=query_list && query_list.size()>0) {
			return query_list.get(0);
		} else {
			return null;
		}
	}


	//根据 设备库线数查询 设备配备表
	public DcEquipmentEquip findEquipmentByParams(String phase_id,String content_id,int jckxs) {
		String jckxsStr = jckxs+"line";
		Criteria criteria = Cnd.NEW();
		criteria.where().and("del_flag", "=", false).and("project_design_phase_id", "=", phase_id)
				.and("project_design_content_id", "=", content_id)
				.and("equipment_equip_code","like","%"+jckxsStr);
		List <DcEquipmentEquip> query_list = query(criteria);
		if (null!=query_list && query_list.size()>0) {
			return query_list.get(0);
		} else {
			return null;
		}
	}

	//项目设备配备表维护
	public QueryResult findEquipmentEquip(NutMap nutMap){
		List <Map> page_list = null;
		Pager pager = null;
		StringBuffer stringBuffer = new StringBuffer(" select dee.*,  "
				+ " dpdp.project_design_phase_name , dpdc.project_design_content_name , dpdm.project_design_major_name  "
				+ " from dc_equipment_equip dee "
				+ " left join dc_project_design_phase dpdp on dee.project_design_phase_id = dpdp.id and dpdp.del_flag = '0'"
				+ " left join dc_project_design_content dpdc on dee.project_design_content_id = dpdc.id and dpdc.del_flag = '0'"
				+ " left join dc_project_design_major dpdm on dee.project_design_major_id = dpdm.id and dpdm.del_flag = '0' "
				+ " where dee.del_flag = '0' ");
		if(nutMap.getString("equip_code") != null){
			stringBuffer.append(" and dee.equipment_equip_code = @equip_code");
		}
		if(nutMap.getString("equip_name") != null){
			stringBuffer.append(" and dee.equipment_equip_name like @equip_name");
		}
		if(nutMap.getString("design_phase_id") != null){
			stringBuffer.append(" and dee.project_design_phase_id = @design_phase_id ");
		}
		if(nutMap.getString("design_content_id") != null){
			stringBuffer.append(" and dee.project_design_content_id = @design_content_id");
		}
		Sql find_sql = Sqls.create(stringBuffer.toString());
		if(nutMap.getString("equip_code") != null){
			find_sql.params().set("equip_code", nutMap.getString("equip_code"));
		}
		if(nutMap.getString("equip_name") != null){
			find_sql.params().set("equip_name", "%"+nutMap.getString("equip_name")+"%");
		}
		if(nutMap.getString("design_phase_id") != null){
			find_sql.params().set("design_phase_id", nutMap.getString("design_phase_id"));
		}
		if(nutMap.getString("design_content_id") != null){
			find_sql.params().set("design_content_id", nutMap.getString("design_content_id"));
		}
		if(nutMap.get("page_num") != null && nutMap.get("page_size") != null){
			pager = new Pager(nutMap.getInt("page_num"), nutMap.getInt("page_size"));
			pager.setRecordCount(findEquipmentEquipNum(nutMap));
			find_sql.setPager(pager);
		}
		find_sql.setCallback(new SqlCallback() {
			List <Map> list = new ArrayList<Map>();
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()) {
					Map map = new HashMap<>();
					map.put("id", rs.getString("id"));
					map.put("equipment_equip_code", rs.getString("equipment_equip_code"));
					map.put("equipment_equip_name", rs.getString("equipment_equip_name"));
					map.put("create_time", rs.getString("create_time"));
					map.put("update_time", rs.getString("update_time"));
					map.put("project_design_phase_name", rs.getString("project_design_phase_name"));
					map.put("project_design_content_name", rs.getString("project_design_content_name"));
					map.put("project_design_major_name", rs.getString("project_design_major_name"));
					list.add(map);
				}
				SwapUtil.clearNull(list);
				return list;
			}
		});
		dao().execute(find_sql);
		page_list = find_sql.getList(Map.class);
		return new QueryResult(page_list, pager);
	}
	
	public int findEquipmentEquipNum(NutMap nutMap){
		StringBuffer stringBuffer = new StringBuffer(" select count(dee.id) num  from dc_equipment_equip dee  where dee.del_flag = '0' ");
		if(nutMap.getString("equip_code") != null){
			stringBuffer.append(" and dee.equipment_equip_code = @equip_code");
		}
		if(nutMap.getString("equip_name") != null){
			stringBuffer.append(" and dee.equipment_equip_name like @equip_name");
		}
		Sql find_sql = Sqls.create(stringBuffer.toString());
		if(nutMap.getString("equip_code") != null){
			find_sql.params().set("equip_code", nutMap.getString("equip_code"));
		}
		if(nutMap.getString("equip_name") != null){
			find_sql.params().set("equip_name", "%"+nutMap.getString("equip_name")+"%");
		}
		if(nutMap.getString("design_phase_id") != null){
			find_sql.params().set("design_phase_id", nutMap.getString("design_phase_id"));
		}
		if(nutMap.getString("design_content_id") != null){
			find_sql.params().set("design_content_id", nutMap.getString("design_content_id"));
		}
		find_sql.setCallback(new SqlCallback() {
			int num = 0;
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()) {
					num = rs.getInt("num");
				}
				return num;
			}
		});
		dao().execute(find_sql);
		return find_sql.getInt();
	}

}
